//
//  LFTabBarItem.m
//  College
//
//  Created by LF on 2018/11/26.
//  Copyright © 2018 MuXi. All rights reserved.
//

#import "LFTabBarItem.h"

static NSString *normalColor = @"#111111";
static NSString *selectedColor = @"0xff6977";

@interface LFTabBarItem()
@property (nonatomic, strong) UIImageView *imageView; /**< 图 */
@property (nonatomic, strong) UILabel *label; /**< 文 */
@property (nonatomic, strong) UILabel *countLabel; /**< 文 */
@property (nonatomic, strong) NSDictionary *data; /**< 数据 */
@end

@implementation LFTabBarItem

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.imageView];
        [self addSubview:self.label];
        [self addSubview:self.countLabel];
        
        CGFloat width = frame.size.width;
        CGFloat height = frame.size.height;
        CGFloat imgW = 24;
        
        self.imageView.frame = CGRectMake((width - imgW) * 0.5, 5, imgW, imgW);
        self.label.frame = CGRectMake(0, height * 0.5 + 5, width, 16);
        self.countLabel.frame = CGRectMake((self.imageView.frame.origin.x + self.imageView.frame.size.width) - 6, self.imageView.frame.origin.y - 2, 14, 14);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
        [self addGestureRecognizer:tap];
        
        [self addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
#pragma mark - 事件
- (void) didTap:(UITapGestureRecognizer *)tap {
    self.selected = YES;
    // 增加点击动画效果
    [self.imageView transform3Ds:@[@0.5,@1.0] withDuration:0.1];
}
- (void)setMessageCount:(NSInteger)messageCount {
    self.countLabel.text = [NSString stringWithFormat:@"%zd",messageCount];
    self.countLabel.hidden = messageCount < 1;
}
#pragma mark - observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (self.isSelected) {
        self.imageView.image = [UIImage imageNamed:self.data[@"selectedImage"]];
        self.label.textColor = [UIColor lf_colorWithHexString:selectedColor];
    } else {
        self.imageView.image = [UIImage imageNamed:self.data[@"image"]];
        self.label.textColor = [UIColor lf_colorWithHexString:normalColor];
    }
    if (context == nil) {
        if (self.isSelected && self.delegate && [self.delegate respondsToSelector:@selector(didSelectItem:)]) {
            [self.delegate didSelectItem:self];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
#pragma mark - 数据
- (void) lf_setItem:(NSDictionary *)data {
    self.data = data;
    self.label.text = data[@"title"];
    self.imageView.image = [UIImage imageNamed:data[@"image"]];
}
#pragma mark - 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}
- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:13];
    }
    return _label;
}
- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.font = [UIFont systemFontOfSize:10];
        _countLabel.hidden = YES;
        _countLabel.textColor = UIColor.whiteColor;
        _countLabel.backgroundColor = UIColor.redColor;
        _countLabel.layer.masksToBounds = YES;
        _countLabel.layer.cornerRadius = 7.f;
        _countLabel.layer.borderWidth = 0.5f;
        _countLabel.layer.borderColor = [UIColor.whiteColor CGColor];
    }
    return _countLabel;
}
#pragma mark - dealloc
- (void)dealloc {
    [self removeObserver:self forKeyPath:@"selected"];
}

@end
